package tesis.common.security.service;

import tesis.common.security.SeguridadService;
import tesis.common.utils.ControllerUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;

/**
 *
 * @author diego.gil
 */
public class CustomRememberMeServices extends AbstractTokenBasedRememberMeServices {

    private static final Logger LOG = LoggerFactory.getLogger(CustomRememberMeServices.class);
    private SeguridadService seguridadService;

    public SeguridadService getSeguridadService() {
        return seguridadService;
    }

    public void setSeguridadService(SeguridadService seguridadService) {
        this.seguridadService = seguridadService;
    }

    @Override
    protected Authentication doCustomAutoLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        LOG.debug("doCustomAutoLogin");
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (Exception e) {
            throw e;
        }
        HttpSession sesion = request.getSession(false);
        if (sesion != null) {
            String username = obtainUsername(sesion);
            String password = obtainPassword(sesion);
            String fromIntranet = obtainFromIntranet(sesion);
            LOG.debug("username [{}] password [{}] fromIntranet [{}]", new Object[]{username, password, fromIntranet});
            if (username == null) {
                return null;
            }
            if (password == null) {
                password = "";
                //QUITAR LUEGO
                UserDetails loadUserByUsername = this.getUserDetailsService().loadUserByUsername(username);
                LOG.debug("loadUserByUsername.getPassword():" + loadUserByUsername.getPassword());
                password = seguridadService.desencriptar(username, loadUserByUsername.getPassword());

                if ("S".equals(fromIntranet)) {
                    sesion.setAttribute("j_password", password);
                    request.getRequestDispatcher("login.action").forward(request, response);
                    return null;
                }
            }
            username = username.trim();
            LOG.debug("username:" + username + ", password:" + password);
            return new UsernamePasswordAuthenticationToken(username, password);
        }
        return null;
    }

    @Override
    public void onLoginSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication successfulAuthentication) {
        LOG.debug("onLoginSuccess");
        super.onLoginSuccess(request, response, successfulAuthentication);
    }

    protected String obtainUsername(HttpSession sesion) {
        return ControllerUtil.obtainSessionAttribute(sesion, "j_username", String.class);
    }

    protected String obtainPassword(HttpSession sesion) {
        return ControllerUtil.obtainSessionAttribute(sesion, "j_password", String.class);
    }

    protected String obtainFromIntranet(HttpSession sesion) {
        return ControllerUtil.obtainSessionAttribute(sesion, "fromIntranet", String.class);
    }
}
